#include "gameover.hpp"

static const int STRLEN = 9;
static const char *LETTSTRS[STRLEN] = 
{
	"g", "a", "m", "e", 0, "o", "v", "e", "r"
};

#include "image.hpp"

static Image *lettimgs[STRLEN];
static Image *fontimg;

#include "camera.hpp"
#include "music.hpp"

bool GameOverScreen::Open()
{
	scalex = 0;
	cursor = 0;

	AllImgs.BatchGet(lettimgs, STRLEN, "logo/", LETTSTRS);
	fontimg = AllImgs.Get("font");
	Cam.Min(0, 0);
	Cam.Max(Cam.size.x, Cam.size.y);
	Cam.Pos(0, 0);
	Cam.Out(0);
	
	Mus.Play(Mus.GAMEOVER);
	return true;
}

void GameOverScreen::Close()
{
	AllImgs.Clear();
}

enum
{
	CONTINUE,
	END,
	OPTCT
};

#include "input.hpp"

#include "game.hpp"
#include "title.hpp"
#include "app.hpp"

void GameOverScreen::In()
{
	if (scalex < STRLEN)
	{
		if (Inp.EdgeBits())
			scalex = STRLEN;
	}
	else
	{
		int dcursor = Inp.CmdEdge(Inp.DOWN) - Inp.CmdEdge(Inp.UP);
		if (dcursor)
		{
			cursor += dcursor; 
			cursor = (cursor % OPTCT + OPTCT) % OPTCT;
		}
		if (Inp.CmdEdge(Inp.ENTER))
		{
			Game.SameStageReset();
			Game.StatReset();
			Screen *NEXTSCRN[OPTCT] =
			{
				&Game,
				&Title
			};

			App.ScreenChange(NEXTSCRN[cursor]);
		}
	}
}

void GameOverScreen::Update()
{
	if (scalex < STRLEN)
		scalex += .125f;
}

#include "video.hpp"

void GameOverScreen::Out(float upfrac)
{
	float sx = scalex;
	float centx = 4;
	const float POSY = 12;
	for (int i = 0; i < scalex; ++i)
	{
		if (lettimgs[i])
		{
			float posx = centx;
			float lsx = std::min(sx, 1.f);
			Vid.Scale(lsx, 1);
			//posx -= .5f * lsx * lettimgs[i]->TileWidth();
	
			Vid.Pos(posx, POSY);
			Vid.Pri(1);
			Vid.Flip(false, false);
			Vid.SpriteDraw(*lettimgs[i], 0);
		}

		--sx;
		centx += 4; 
	}

	Vid.Scale(1, 1);

	if (scalex < STRLEN)
		return;
	
	const wchar_t *OPTTEXT[OPTCT] =
	{
		L"CONTINUE",
		L"END"
	};

	const float POSX = 16;
	float posy = 20;

	for (int i = 0; i < OPTCT; ++i)
	{
		Vid.Pos(POSX, posy);
		if (cursor == i)
			Vid.Color(.5f, .5f, 1, 1);
		else
			Vid.Color(.5f, .5f, .5f, 1);
		Vid.TilesDraw(*fontimg, OPTTEXT[i], std::wcslen(OPTTEXT[i]), 1);
		posy += 4;
	}

	Vid.Color(1, 1, 1, 1);
}

GameOverScreen GameOver;
